<?php
tsload ( APPS_PATH . '/admin/Lib/Action/AdministratorAction.class.php' );
class ScoreAction extends AdministratorAction {
	
	
	public function index(){//【提现申请列表】
	$this->_initWeibaListAdminMenu();
	
	$this->pageButton[] = array('title'=>'搜索记录','onclick'=>"admin.fold('search_form')");
	$this->pageButton[] = array('title'=>L('PUBLIC_REMOVE_COMPLETELY'),'onclick'=>"admin.delMoneys('','delMoneys','".L('PUBLIC_REMOVE_COMPLETELY')."','".L('提现记录')."')");
	
	$this->pageKeyList = array('id','uid','credit_consume','money','card_num','card_name','name','apply_time','end_time','allow_status','status','DOACTION');
	$map = array();
	$this->searchKey = array('uid',array('money','money1'),'card_num','name','allow_status','status');
		$this->opt['type'] = array('0'=>'充值类型','1'=>'手机充值','2'=>'流量充值','3'=>'游戏充值');
		$this->opt['allow_status'] = array('0'=>'全部','1'=>'未通过','2'=>'已通过');
		$this->opt['status'] = array('0'=>'全部','1'=>'未处理','2'=>'已处理');
		$map = array();
		$_POST['card_num'] && $map['card_num']=base64_encode($_POST['card_num']);
		$_POST['uid'] && $map['uid']=intval($_POST['uid']);
		$_POST['orderid'] && $map['orderid']=$_POST['orderid'];
		$_POST['allow_status'] && $map['allow_status']=$_POST['allow_status']=='1'?'0':$_POST['allow_status']=='2'?'1':'';
		$_POST['status'] && $map['status']=$_POST['status']=='1'?'0':$_POST['status']=='2'?'1':'';
		if (! empty ( $_POST ['money'] )) {
				if (! empty ( $_POST ['money'] [0] ) && ! empty ( $_POST ['money'] [1] )) {
					// 时间区间条件
					$map ['money'] = array (
							'BETWEEN',
							array (
									$_POST ['money'] [0],
									$_POST ['money'] [1] 
							) 
					);
				} else if (! empty ( $_POST ['money'] [0] )) {
					// 时间大于条件
					$map ['money'] = array (
							'GT',
							$_POST ['money'] [0] 
					);
				} elseif (! empty ( $_POST ['money'] [1] )) {
					// 时间小于条件
					$map ['money'] = array (
							'LT',
							$_POST ['money'] [1]
					);
				}
			}
			//dump($map);
	$this->_listpk = 'id';
	$listData = $this->getMoneyList(20,$map);
	$this->displayList($listData);
	}
	
	public function getMoneyList($limit='20',$map){
		$list = M("User_to_money")->where($map)->order("id desc")->findPage($limit);
		foreach($list['data'] as $k => $v) {
			$list['data'][$k]['card_num'] = base64_decode($v['card_num']);
			$list['data'][$k]['apply_time'] = date("Y-m-d h:i:s",$v['apply_time']);
			$list['data'][$k]['end_time'] = $v['end_time']=='0'?'------':date("Y-m-d h:i:s",$v['end_time']);
			$list['data'][$k]['allow_status'] = $v['allow_status']=='0'?'未通过':'已通过';
			$list['data'][$k]['status'] = $v['status']=='0'?'未处理':'已处理';
			$list['data'][$k]['DOACTION'] = '<a href="'.U('admin/Score/editMoney',array('id'=>$v['id'],'tabHash'=>'editMoney')).'">处理</a>-<a onclick="admin.delMoney('.$v['id'].');" href="javascript:void(0)">删除</a>';

		}
		
		return $list;
	
	}
	
	public function editMoney(){//【处理申请】
		$id = $_REQUEST['id'];
		$this->_initWeibaListAdminMenu();
		$this->pageKeyList = array('id','uid','credit_consume','money','card_num','card_name','name','apply_time','end_time','allow_status','status');
		$data =  M("User_to_money")->where("id = $id")->find();
//		$user = M("User")->where('uid='.$data['uid'])->find();
//		$this->opt['uname'] = $user['uname'];
		$data['apply_time'] = date("Y-m-d h:i:s",$data['apply_time']);
		$data['card_num'] = base64_decode($data['card_num']);
		$data['end_time'] = $data['end_time']=='0'?'-----':date("Y-m-d h:i:s",$data['end_time']);
		$this->opt['allow_status'] = array('0'=>'未通过','1'=>'通过');
		$this->opt['status'] = array('0'=>'未处理','1'=>'已处理');
		
		// 表单URL设置
		$this->savePostUrl = U('admin/Score/doEditMoney');
       
		$this->displayConfig($data);
	
	}
	public function doEditMoney(){//【执行处理操作】
		$id = $_REQUEST['id'];
		$allow_status = $_REQUEST['allow_status'];
		$status = $_REQUEST['status'];
		$credit_user = M("Credit_user");
		$money = M("User_to_money");
		$old = $money->where("id = $id")->find();
		$money->id = $id;
		
		if($allow_status){//通过，已处理
			if($old['allow_status']=='1' && $old['status']=='1'){
				$this->assign('jumpUrl', U('admin/Score/editMoney',array('id'=>$id,'tabHash' => 'editMoney')));
				$this->success(L('操作成功'));
			}
			if($old['allow_status']=='0' && $old['status']=='1'){
				
				$old_credit = $credit_user->where('uid='.$old['uid'])->find();
				$new_credit = $old_credit['score']-$old['credit_consume'];
				if($new_credit < 0){//积分不足扣，操作失败
					$this->assign('jumpUrl', U('admin/Score/editMoney',array('id'=>$id,'tabHash' => 'editMoney')));
					$this->error(L('操作失败，用户积分不足'));
				}
				$credit_user->uid = $old['uid'];
				$credit_user->score = $new_credit;
				$credit_user->save();
			}
			$money->allow_status = 1;
			$money->status = 1;
			$money->end_time = time();
		}elseif ($status){//未通过，已处理
			if($old['allow_status']=='0' && $old['status']=='1'){
				$this->assign('jumpUrl', U('admin/Score/editMoney',array('id'=>$id,'tabHash' => 'editMoney')));
				$this->success(L('操作成功'));
			}
				$old_credit = $credit_user->where('uid='.$old['uid'])->find();
				$new_credit = $old_credit['score']+$old['credit_consume'];
				$credit_user->uid = $old['uid'];
				$credit_user->score = $new_credit;
				$credit_user->save();
			
			$money->allow_status = 0;
			$money->status = 1;
			$money->end_time = time();
		}else{//还原
			if($old['allow_status']=='0' && $old['status']=='0'){
				$this->assign('jumpUrl', U('admin/Score/editMoney',array('id'=>$id,'tabHash' => 'editMoney')));
				$this->success(L('操作成功'));
			}
			if($old['allow_status']=='0' && $old['status']=='1'){//扣除积分
				$old_credit = $credit_user->where('uid='.$old['uid'])->find();
				$new_credit = $old_credit['score']-$old['credit_consume'];
				if($new_credit < 0){//积分不足扣，操作失败
					$this->assign('jumpUrl', U('admin/Score/editMoney',array('id'=>$id,'tabHash' => 'editMoney')));
					$this->error(L('操作失败，用户积分不足'));
				}
				$credit_user->uid = $old['uid'];
				$credit_user->score = $new_credit;
				$credit_user->save();
			}
			
			$money->allow_status = 0;
			$money->status = 0;
			$money->end_time = '';
		}
		
		$result = $money->save();
		
		if($result){
			$this->assign('jumpUrl', U('admin/Score/editMoney',array('id'=>$id,'tabHash' => 'editMoney')));
			$this->success(L('操作成功'));
		}else{
			$this->assign('jumpUrl', U('admin/Score/editMoney',array('id'=>$id,'tabHash' => 'editMoney')));
			$this->error(L('操作失败'));
		}
	}
	
	public function delMoney(){
		$id = $_POST['id'];
		$map['id'] = is_array($id) ? array('IN', $id) : intval($id);
		$result = M('User_to_money')->where($map)->delete();
			$return['status'] = 1;
			$return['data']   = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
		echo json_encode($return);exit();
	
	}
	
	public function userScore(){//【用户积分】
		$this->pageButton[] = array('title'=>'条件搜索','onclick'=>"admin.fold('search_form')");
		$this->pageKeyList = array('id','uid','uname','score','money','DOACTION');
		
		$map = array();
		$this->searchKey = array('uid','uname',array('score','score1'));
			
			$_POST['uid'] && $map['uid']=intval($_POST['uid']);
			//$_POST['uname'] && $map['uname']=array('like','%'.t($_POST['uname']).'%');
			if($_POST['uname']){
				$maps['uname']=array('like','%'.t($_POST['uname']).'%');
				$user = M("User")->where($maps)->field("uid")->select();
				
				foreach ($user as $u){
					$uids[] = $u['uid'];
				}
				if(!empty($uids)){
					$map['uid']=array('in',$uids);
				}
				
			}
			if (! empty ( $_POST ['score'] )) {
					if (! empty ( $_POST ['score'] [0] ) && ! empty ( $_POST ['score'] [1] )) {
						// 时间区间条件
						$map ['score'] = array (
								'BETWEEN',
								array (
										$_POST ['score'] [0],
										$_POST ['score'] [1] 
								) 
						);
					} else if (! empty ( $_POST ['score'] [0] )) {
						// 时间大于条件
						$map ['score'] = array (
								'GT',
								$_POST ['score'] [0] 
						);
					} elseif (! empty ( $_POST ['score'] [1] )) {
						// 时间小于条件
						$map ['score'] = array (
								'LT',
								$_POST ['score'] [1]
						);
					}
				}
		
		
		
		$this->_listpk = 'id';
		$listData = $this->getScoreList(20,$map);
		$this->displayList($listData);
	
	}
	public function getScoreList($limit,$map){//【用户积分列表】

		$score = M("Credit_user");

		$rate = model("Xdata")->getConfig("to_rate","scoreSetting","admin_Score");
		$list = $score->where($map)->limit($limit)->order("id desc")->findPage();
		foreach($list['data'] as $k => $v) {
			$info = M("User")->where('uid = '.$v['uid'])->find();
			$list['data'][$k]['uname'] = $info['uname'];
			$list['data'][$k]['money'] = $v['score']/$rate;
	
			$list['data'][$k]['DOACTION'] = '<a href="'.U('admin/Score/editScore',array('id'=>$v['id'],'tabHash'=>'editScore')).'">修改</a>';

		}
		return $list;
	}
	public function editScore(){//【修改积分】
		$id = $_REQUEST['id'];
		$this->pageKeyList = array('id','uid','uname','score');
		$score = M("Credit_user");
		$data = $score->where("id = $id")->find();
		$info = M("User")->where('uid = '.$data['uid'])->find();
		$data['uname'] = $info['uname'];
		
		$this->savePostUrl = U('admin/Score/doEditScore');
        $this->notEmpty = array("score");
		$this->onsubmit = 'admin.addScoreSubmitCheck(this)';
		
		$this->displayConfig($data);
	}
	
	public function doEditScore(){//【修改积分】
		$id = $_REQUEST['id'];
		$credit= $_REQUEST['score'];
		$score = M("Credit_user");
		$score->id = $id;
		$score->score = $credit;
		if($score->save()){
			$this->assign('jumpUrl', U('admin/Score/editScore',array('id'=>$id,'tabHash' => 'editScore')));
			$this->success(L('操作成功'));
		}else{
			$this->assign('jumpUrl', U('admin/Score/editScore',array('id'=>$id,'tabHash' => 'editScore')));
			$this->error(L('操作失败'));
		}
		
	}
	
	
	public function scoreSetting(){//【提现设置】
	$this->_initWeibaListAdminMenu();
	$this->pageTitle ['scoreSetting'] = '积分提现设置';
	$this->pageKeyList = array('to_money_status','to_rate','to_min');
	$this->opt['to_money_status'] = array('0'=>'关闭','1'=>'开启');
	$this->displayConfig();
	}
	private function _initWeibaListAdminMenu(){
		$this->pageTab[] = array('title'=>'提现申请','tabHash'=>'index','url'=>U('admin/Score/index'));
		$this->pageTab[] = array('title'=>'提现设置','tabHash'=>'addshop','url'=>U('admin/Score/scoreSetting'));
	}
	
	private function _initRechargeAdminMenu(){
		$this->pageTab[] = array('title'=>'充值列表','tabHash'=>'index','url'=>U('admin/Score/rechargeLog'));
		$this->pageTab[] = array('title'=>'充值设置','tabHash'=>'addshop','url'=>U('admin/Score/rechargeSetting'));
	}
	
	public function rechargeLog(){
		$this->_initRechargeAdminMenu();
		$this->pageButton[] = array('title'=>'搜索记录','onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>L('PUBLIC_REMOVE_COMPLETELY'),'onclick'=>"admin.delRecharges('','delRecharges','".L('PUBLIC_REMOVE_COMPLETELY')."','".L('充值记录')."')");
		
		$this->searchKey = array('uid','orderid','type','sporder_id',array('ctime','ctime1'));
		$this->opt['type'] = array('0'=>'全部','1'=>'手机充值','2'=>'流量充值','3'=>'游戏充值');
		$map = array();
		$_POST['uid'] && $map['uid']=intval($_POST['uid']);
		$_POST['orderid'] && $map['orderid']=intval($_POST['orderid']);
		if($_POST['type']=='1'){
			$map['type']='手机';
		}elseif ($_POST['type']=='2'){
			$map['type']='流量';
		}elseif ($_POST['type']=='3'){
			$map['type']='游戏';
		}
		if (! empty ( $_POST ['ctime'] )) {
				if (! empty ( $_POST ['ctime'] [0] ) && ! empty ( $_POST ['ctime'] [1] )) {
					// 时间区间条件
					$map ['ctime'] = array (
							'BETWEEN',
							array (
									strtotime ( $_POST ['ctime'] [0] ),
									strtotime ( $_POST ['ctime'] [1] ) 
							) 
					);
				} else if (! empty ( $_POST ['ctime'] [0] )) {
					// 时间大于条件
					$map ['ctime'] = array (
							'GT',
							strtotime ( $_POST ['ctime'] [0] ) 
					);
				} elseif (! empty ( $_POST ['ctime'] [1] )) {
					// 时间小于条件
					$map ['ctime'] = array (
							'LT',
							strtotime ( $_POST ['ctime'] [1] ) 
					);
				}
			}
	
		$this->pageKeyList = array('id','uid','orderid','type','phone','credit','money','flow','sporder_id','ctime','DOACTION');
		$this->_listpk = 'id';
		$listData = $this->getRechargeList(20,$map);
		$this->displayList($listData);
		
	}
	
	public function getRechargeList($limit='20',$map){
		$list = M("Recharge")->where($map)->order("id desc")->findPage($limit);
		foreach($list['data'] as $k => $v) {
			$list['data'][$k]['type'] = $v['type'].'充值';
			$list['data'][$k]['ctime'] = date("Y-m-d h:i:s",$v['ctime']);
			$list['data'][$k]['DOACTION'] = '<a onclick="admin.delRecharge('.$v['id'].');" href="javascript:void(0)">删除</a>';

		}
		
		return $list;
	
	}
	
	public function rechargeSetting(){
		$this->_initRechargeAdminMenu();
		$this->pageKeyList = array('openid','phone_key','flow_key','game_key','phone_status','flow_status','game_status');
		$this->opt['phone_status'] = array('0'=>'关闭','1'=>'开启');
		$this->opt['flow_status'] = array('0'=>'关闭','1'=>'开启');
		$this->opt['game_status'] = array('0'=>'关闭','1'=>'开启');
		$this->displayConfig();
	}
	
	
	public function delRecharge(){//【删除充值记录】
		$id = $_POST['id'];
		$map['id'] = is_array($id) ? array('IN', $id) : intval($id);
		$result = M('Recharge')->where($map)->delete();
			$return['status'] = 1;
			$return['data']   = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
		echo json_encode($return);exit();
	}
	
	
	
	
	
	
	
	
	
}